package braden.cis.uab.edu.grammar;

import java.util.ArrayList;

import braden.cis.uab.edu.automaton.Test;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class CFGEditorActivity extends Activity {

	private LinearLayout linearLayout;
	private ScrollView scrollView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		linearLayout = new LinearLayout(this);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		scrollView = new ScrollView(this);
		scrollView.addView(linearLayout);

		Button addRuleButton = new Button(this);
		addRuleButton.setText("Add Rule");
		addRuleButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				addEmptyRule();
			}
		});
		linearLayout.addView(addRuleButton);

		setContentView(scrollView);
		
		if (Test.grammar != null) {
			showGrammar(Test.grammar);
		}
	}
	
	public void showGrammar(ArrayList<Rule> grammarRules) {
		
		for (Rule rule : grammarRules) {
			addRule(rule);
		}
	}

	public void addEmptyRule() {

		final LinearLayout ruleLayout = new LinearLayout(this);
		ruleLayout.setOrientation(LinearLayout.HORIZONTAL);
		ruleLayout.setGravity(Gravity.CENTER);

		EditText variableBox = new EditText(this);
		variableBox.setHint("variable");
		InputFilter[] filterArray = new InputFilter[2];
		filterArray[0] = new InputFilter.LengthFilter(1);
		filterArray[1] = new InputFilter.AllCaps();
		variableBox.setFilters(filterArray);
		ruleLayout.addView(variableBox);

		TextView arrow = new TextView(this);
		arrow.setText("--->");
		ruleLayout.addView(arrow);

		EditText terminalBox = new EditText(this);
		terminalBox.setHint("results separated by |");
		ruleLayout.addView(terminalBox);

		Button deleteRuleButton = new Button(this);
		deleteRuleButton.setText("X");
		deleteRuleButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				linearLayout.removeView(ruleLayout);
			}
		});
		ruleLayout.addView(deleteRuleButton);

		linearLayout.addView(ruleLayout);
	}
	
	public void addRule(Rule rule) {

		final LinearLayout ruleLayout = new LinearLayout(this);
		ruleLayout.setOrientation(LinearLayout.HORIZONTAL);
		ruleLayout.setGravity(Gravity.CENTER);
		
		if (rule.isStart()) {
			TextView arrow = new TextView(this);
			arrow.setText("->");
			ruleLayout.addView(arrow);
		}

		EditText variableBox = new EditText(this);
		
		InputFilter[] filterArray = new InputFilter[2];
		filterArray[0] = new InputFilter.LengthFilter(1);
		filterArray[1] = new InputFilter.AllCaps();
		variableBox.setFilters(filterArray);
		//the ""+ at the beginning of the setText method is an odd but necessary fix due to a bug in the method.
		variableBox.setText(""+rule.getVariable());
		ruleLayout.addView(variableBox);

		TextView arrow = new TextView(this);
		arrow.setText("--->");
		ruleLayout.addView(arrow);

		String results = "";
		for (char[] chars : rule.getResults()) {
			results += new String(chars) + "|";
		}
		results = results.substring(0, results.length() - 1);
		
		EditText terminalBox = new EditText(this);
		terminalBox.setText(results);
		ruleLayout.addView(terminalBox);

		Button deleteRuleButton = new Button(this);
		deleteRuleButton.setText("X");
		deleteRuleButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				linearLayout.removeView(ruleLayout);
			}
		});
		ruleLayout.addView(deleteRuleButton);

		linearLayout.addView(ruleLayout);
	}
}
